layui.use(['form','layer','jquery'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer
        $ = layui.jquery;
    
    
    $('.imgcode').click(function() {
    	debugger;
		var url = ctx + "captcha/captchaImage?type=" + captchaType + "&s=" + Math.random();
		$(".imgcode").attr("src", url);
	});
    
    //登录按钮
    form.on("submit(login)",function(data){
    	var index = layer.load();
        $(this).text("登录中...").attr("disabled","disabled").addClass("layui-disabled");
        var btn =  $(this);
        $.ajax({
            type: "post",
            url: ctx + "login",
            data: {
                "username": data.field.username,
                "password": data.field.password,
                "validateCode" : data.field.validateCode,
                "rememberMe": true
            },
            success: function(r) {
            	btn.text("登录").removeAttr("disabled").removeClass("layui-disabled");
                if (r.code == 0) {
                    location.href = ctx + 'index';
                } else {
                	layer.close(index); 
                	$('.imgcode').click();
                	layer.msg(r.msg);
                }
            }
        });
        
        return false;
    })

    //表单输入效果
    $(".loginBody .input-item .layui-input").click(function(e){
        e.stopPropagation();
        $(this).addClass("layui-input-focus").find(".layui-input").focus();
    })
    $(".loginBody .layui-form-item .layui-input").focus(function(){
        $(this).parent().addClass("layui-input-focus");
    })
    $(".loginBody .layui-form-item .layui-input").blur(function(){
        $(this).parent().removeClass("layui-input-focus");
        if($(this).val() != ''){
            $(this).parent().addClass("layui-input-active");
        }else{
            $(this).parent().removeClass("layui-input-active");
        }
    })
})
